package com.wzm.manager;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

import com.wzm.bean.MovieInfo;
import com.wzm.cache.ConfigCache;
import com.wzm.moviepic.R;
import com.wzm.utils.Logger;
import com.wzm.utils.UtilsTools;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.os.Environment;

public class AppManager {
	
	private Context mContext;
	private static AppManager instance = null;
	
	private FinalHttp fh = null;
	private FinalBitmap fb = null;
	public synchronized static AppManager getInstance(Context context)
	{
		if(instance == null)
		{
			instance = new AppManager(context);		
		}
		return instance;
	}
	
	AppManager(Context context)
	{
		mContext = context;
		//初始化
		
		initApp();
	}
	
	public void initApp()
	{
		fh = new FinalHttp();
		fb = FinalBitmap.create(mContext,Environment.getExternalStorageDirectory().getPath()+AppConfig.IMGCACHE);
		//fb = FinalBitmap.create(mContext);
		fb.configLoadingImage(R.drawable.story_default);
		fb.configLoadfailImage(R.drawable.story_default);
		//不压缩
		//fb.configCalculateBitmapSizeWhenDecode(true);
		
	}

	public FinalHttp getFh() {
		return fh;
	}
	
	//只用做电影图片
	public FinalBitmap getFb() {
		return fb;
	}
	
	//电影列表
	private ArrayList<MovieInfo> mList = new ArrayList<MovieInfo>();
	public ArrayList<MovieInfo> getmList() {
		return mList;
	}

	public void setmList(ArrayList<MovieInfo> mList) {
		this.mList = mList;
	}
	

	//MAP(记录顶，看，踩次数)  KEY-MOVIEID  value--次数
	List<String> dingList = new ArrayList<String>();
	List<String> caiList = new ArrayList<String>();;
	List<String> playedList = new ArrayList<String>();
	public List<String> getDingList() {
		return dingList;
	}

	public void setDingList(List<String> dingList) {
		this.dingList = dingList;
	}

	public List<String> getCaiList() {
		return caiList;
	}

	public void setCaiList(List<String> caiList) {
		this.caiList = caiList;
	}

	public List<String> getPlayedList() {
		return playedList;
	}

	public void setPlayedList(List<String> playedList) {
		this.playedList = playedList;
	};
	
	//保存观看的数据
	public void saveInfo()
	{
		//保存数据                    	   
 	   try {
     		JSONObject post = new JSONObject();
			post.put("ding",UtilsTools.ListToJson(AppManager.getInstance(mContext).getDingList()));			
			post.put("cai", UtilsTools.ListToJson(AppManager.getInstance(mContext).getCaiList()));
			post.put("played", UtilsTools.ListToJson(AppManager.getInstance(mContext).getPlayedList()));			
			Logger.error(post.toString());
			//保存
			ConfigCache.setUrlCache(post.toString(),AppConfig.TAG_URL);
			post = null;
				
 	   } catch (JSONException e) {
 		    Logger.error("save info failed:"+e.getMessage());
 	   }
	}
	
	//发送观看顶踩的数据
	public void postsaveInfo()
	{
		String postData = ConfigCache.getUrlTagCache(AppConfig.TAG_URL);
		if(null!=postData)
		{
			
			AjaxParams params = new AjaxParams();
			params.put("json_data",postData);

			fh.post(AppConfig.TAG_URL,params,new AjaxCallBack<Object>() {

				@Override
				public void onSuccess(Object t) {
					// TODO Auto-generated method stub
					super.onSuccess(t);
					ConfigCache.delCache(AppConfig.TAG_URL);
					Logger.error("postsaveinfo onSuccess"+t.toString());
				}

				@Override
				public void onFailure(Throwable t, String strMsg) {
					// TODO Auto-generated method stub
					super.onFailure(t, strMsg);
					Logger.error("postsaveinfo onFailure"+strMsg);
				}
				
			});
		}		
	}
	
	
	
}
